package com.punai.dataanalysis.epdatakb.service.NG001change1212;

import cn.hutool.core.util.StrUtil;
import com.punai.common.core.domain.BaseEntity;
import com.punai.common.utils.NumberUtil;
import com.punai.dataanalysis.epdatakb.bo.JtczResultBo;
import com.punai.dataanalysis.epdatakb.domain.DataAnalysisNdjsgcxmsjfxhzb;
import com.punai.dataanalysis.epdatakb.mapper.NG001change1212.YearGcXmCbC1212Mapper;
import com.punai.dataanalysis.epdatakb.vo.DataAnalysisVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class NG001YearGcXmCbC1212Service {

    @Autowired
    private YearGcXmCbC1212Mapper yearGcXmCbC1212Mapper;

    public List<? extends BaseEntity> getDataList(DataAnalysisVo dataAnalysisVo) {
        Map<String,Object> params = new HashMap<>();
        params.put("fgs",dataAnalysisVo.getFgs());
        params.put("month1",dataAnalysisVo.isMonth1());
        List<DataAnalysisNdjsgcxmsjfxhzb> infos = yearGcXmCbC1212Mapper.getInfos(params);
        infos = new ArrayList<>(infos);
        List<JtczResultBo> bncbhj = yearGcXmCbC1212Mapper.bncbhj(params);
        List<JtczResultBo> bncbzyfb = yearGcXmCbC1212Mapper.bncbzyfb();
        List<JtczResultBo> bncbjgc = yearGcXmCbC1212Mapper.bncbjgc();
        List<JtczResultBo> ljcbhj = yearGcXmCbC1212Mapper.ljcbhj();
        List<JtczResultBo> ljcbzyfb = yearGcXmCbC1212Mapper.ljcbzyfb();

        Map<String, BigDecimal> bncbhjMap = bncbhj.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal, (v1, v2) -> v2));
        Map<String, BigDecimal> bncbzyfbMap = bncbzyfb.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal, (v1, v2) -> v2));
        Map<String, BigDecimal> bncbjgcMap = bncbjgc.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal, (v1, v2) -> v2));
        Map<String, BigDecimal> ljcbhjMap = ljcbhj.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal, (v1, v2) -> v2));
        Map<String, BigDecimal> ljcbzyfbMap = ljcbzyfb.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal, (v1, v2) -> v2));
        // 先吧值放到infos里面
        for (DataAnalysisNdjsgcxmsjfxhzb info : infos) {
            String pc = info.getPc();
            BigDecimal bncbhjVal = bncbhjMap.getOrDefault(pc, BigDecimal.ZERO);
            BigDecimal bncbzyfbVal = bncbzyfbMap.getOrDefault(pc, BigDecimal.ZERO);
            BigDecimal bncbjgcVal = bncbjgcMap.getOrDefault(pc, BigDecimal.ZERO);
            BigDecimal ljcbhjVal = ljcbhjMap.getOrDefault(pc, BigDecimal.ZERO);
            BigDecimal ljcbzyfbVal = ljcbzyfbMap.getOrDefault(pc, BigDecimal.ZERO);
            info.setYearCostHj(bncbhjVal);
            info.setYearCostZyfb(bncbzyfbVal);
            info.setYearCostJgc(bncbjgcVal);
            info.setKlCostHj(ljcbhjVal);
            info.setKlCostZyfb(ljcbzyfbVal);

            calcZb(info);
        }
        DataAnalysisNdjsgcxmsjfxhzb zj = zj(infos);
        zj.setProName("总计");
        infos = xmbFz(infos);
        infos.add(zj);

        return infos;
    }

    private List<DataAnalysisNdjsgcxmsjfxhzb> xmbFz(List<DataAnalysisNdjsgcxmsjfxhzb> res) {
        List<DataAnalysisNdjsgcxmsjfxhzb> newResList = new ArrayList<>();
        Map<String, List<DataAnalysisNdjsgcxmsjfxhzb>> xmbGroup = res.stream().filter(e -> StrUtil.isNotBlank(e.getDeptName())).collect(Collectors.groupingBy(DataAnalysisNdjsgcxmsjfxhzb::getDeptName));
        for (Map.Entry<String, List<DataAnalysisNdjsgcxmsjfxhzb>> entry : xmbGroup.entrySet()) {
            String key = entry.getKey();
            List<DataAnalysisNdjsgcxmsjfxhzb> value = entry.getValue();
            DataAnalysisNdjsgcxmsjfxhzb zj = zj(value);
            zj.setProName(key + "小计");
            newResList.addAll(value);
            newResList.add(zj);
        }
        return newResList;
    }

    private DataAnalysisNdjsgcxmsjfxhzb zj(List<DataAnalysisNdjsgcxmsjfxhzb> value) {
        DataAnalysisNdjsgcxmsjfxhzb re = new DataAnalysisNdjsgcxmsjfxhzb();
        BigDecimal htTotal = BigDecimal.ZERO;
        BigDecimal jzmj = BigDecimal.ZERO;
        BigDecimal yearqkYsCzHj = BigDecimal.ZERO;
        BigDecimal yearqkYsCzZyfb = BigDecimal.ZERO;
        BigDecimal yearqkCzSj = BigDecimal.ZERO;
        BigDecimal yearqkCzJgc = BigDecimal.ZERO;
        BigDecimal klqkCostHj = BigDecimal.ZERO;
        BigDecimal klqkCostZyfb = BigDecimal.ZERO;
        BigDecimal klqkCostJgc = BigDecimal.ZERO;
        BigDecimal klqkCostYearPd = BigDecimal.ZERO;
        BigDecimal yearCzHj = BigDecimal.ZERO;
        BigDecimal yearCzZyfb = BigDecimal.ZERO;
        BigDecimal yearCzSj = BigDecimal.ZERO;
        BigDecimal yearCzJgc = BigDecimal.ZERO;
        BigDecimal yearCostHj = BigDecimal.ZERO;
        BigDecimal yearCostZyfb = BigDecimal.ZERO;
        BigDecimal yearCostJgc = BigDecimal.ZERO;
        BigDecimal yearLr = BigDecimal.ZERO;
        BigDecimal klCzYsHj = BigDecimal.ZERO;
        BigDecimal klCzYsZyfb = BigDecimal.ZERO;
        BigDecimal klCzSj = BigDecimal.ZERO;
        BigDecimal klCzJgc = BigDecimal.ZERO;
        BigDecimal klCostHj = BigDecimal.ZERO;
        BigDecimal klCostZyfb = BigDecimal.ZERO;
        BigDecimal klCostJgc = BigDecimal.ZERO;
        BigDecimal klLr = BigDecimal.ZERO;
        BigDecimal htkhSjzhfy = BigDecimal.ZERO;
        BigDecimal htkhXmdfp = BigDecimal.ZERO;
        for (DataAnalysisNdjsgcxmsjfxhzb jscg : value) {
            htTotal = NumberUtil.add(htTotal, jscg.getHtTotal());
            jzmj = NumberUtil.add(jzmj, jscg.getJzmj());
            yearqkYsCzHj = NumberUtil.add(yearqkYsCzHj, jscg.getYearqkYsCzHj());
            yearqkYsCzZyfb = NumberUtil.add(yearqkYsCzZyfb, jscg.getYearqkYsCzZyfb());
            yearqkCzSj = NumberUtil.add(yearqkCzSj, jscg.getYearqkCzSj());
            yearqkCzJgc = NumberUtil.add(yearqkCzJgc, jscg.getYearqkCzJgc());
            klqkCostHj = NumberUtil.add(klqkCostHj, jscg.getKlqkCostHj());
            klqkCostZyfb = NumberUtil.add(klqkCostZyfb, jscg.getKlqkCostZyfb());
            klqkCostJgc = NumberUtil.add(klqkCostJgc, jscg.getKlqkCostJgc());
            klqkCostYearPd = NumberUtil.add(klqkCostYearPd, jscg.getKlqkCostYearPd());
            yearCzHj = NumberUtil.add(yearCzHj, jscg.getYearCzHj());
            yearCzZyfb = NumberUtil.add(yearCzZyfb, jscg.getYearCzZyfb());
            yearCzSj = NumberUtil.add(yearCzSj, jscg.getYearCzSj());
            yearCzJgc = NumberUtil.add(yearCzJgc, jscg.getYearCzJgc());
            yearCostHj = NumberUtil.add(yearCostHj, jscg.getYearCostHj());
            yearCostZyfb = NumberUtil.add(yearCostZyfb, jscg.getYearCostZyfb());
            yearCostJgc = NumberUtil.add(yearCostJgc, jscg.getYearCostJgc());
            yearLr = NumberUtil.add(yearLr, jscg.getYearLr());
            klCzYsHj = NumberUtil.add(klCzYsHj, jscg.getKlCzYsHj());
            klCzYsZyfb = NumberUtil.add(klCzYsZyfb, jscg.getKlCzYsZyfb());
            klCzSj = NumberUtil.add(klCzSj, jscg.getKlCzSj());
            klCzJgc = NumberUtil.add(klCzJgc, jscg.getKlCzJgc());
            klCostHj = NumberUtil.add(klCostHj, jscg.getKlCostHj());
            klCostZyfb = NumberUtil.add(klCostZyfb, jscg.getKlCostZyfb());
            klCostJgc = NumberUtil.add(klCostJgc, jscg.getKlCostJgc());
            klLr = NumberUtil.add(klLr, jscg.getKlLr());
            htkhSjzhfy = NumberUtil.add(htkhSjzhfy, jscg.getHtkhSjzhfy());
            htkhXmdfp = NumberUtil.add(htkhXmdfp, jscg.getHtkhXmdfp());
        }

        re.setHtTotal(htTotal);
        re.setJzmj(jzmj);
        re.setYearqkYsCzHj(yearqkYsCzHj);
        re.setYearqkYsCzZyfb(yearqkYsCzZyfb);
        re.setYearqkCzSj(yearqkCzSj);
        re.setYearqkCzJgc(yearqkCzJgc);
        re.setKlqkCostHj(klqkCostHj);
        re.setKlqkCostZyfb(klqkCostZyfb);
        re.setKlqkCostJgc(klqkCostJgc);
        re.setKlqkCostYearPd(klqkCostYearPd);
        re.setYearCzHj(yearCzHj);
        re.setYearCzZyfb(yearCzZyfb);
        re.setYearCzSj(yearCzSj);
        re.setYearCzJgc(yearCzJgc);
        re.setYearCostHj(yearCostHj);
        re.setYearCostZyfb(yearCostZyfb);
        re.setYearCostJgc(yearCostJgc);
        re.setYearLr(yearLr);
        re.setKlCzYsHj(klCzYsHj);
        re.setKlCzYsZyfb(klCzYsZyfb);
        re.setKlCzSj(klCzSj);
        re.setKlCzJgc(klCzJgc);
        re.setKlCostHj(klCostHj);
        re.setKlCostZyfb(klCostZyfb);
        re.setKlCostJgc(klCostJgc);
        re.setKlLr(klLr);
        re.setHtkhSjzhfy(htkhSjzhfy);
        re.setHtkhXmdfp(htkhXmdfp);
        re.hb();
        calcZb(re);
        return re;
    }

    private void calcZb(DataAnalysisNdjsgcxmsjfxhzb sjcb) {
        // 往年年终盘点利润情况分析
        BigDecimal yearqkYsCzHj = sjcb.getYearqkYsCzHj();
        BigDecimal yearqkCzJgc = sjcb.getYearqkCzJgc();
        BigDecimal klCostHj = sjcb.getKlqkCostHj();
        // 往年年终盘点利润情况（元） = 往年累计完成产值情况 - 往年累计发生成本情况
        BigDecimal wnpdlr = com.punai.common.utils.NumberUtil.subtract(yearqkYsCzHj, klCostHj);
        sjcb.setKlqkCostYearPd(wnpdlr);
        // 往年年终盘点利润率（%） = 往年年终盘点利润情况（元） / (往年累计完成产值情况 + 往年累计完成产值情况甲供材 )
        BigDecimal wnpdlrl = com.punai.common.utils.NumberUtil.divideUseProportion(wnpdlr, 2, com.punai.common.utils.NumberUtil.add(yearqkYsCzHj, yearqkCzJgc));
        sjcb.setKlqkCostYearPdLrl(wnpdlrl);

        // 本年利润情况分析
        BigDecimal czHj = sjcb.getYearCzHj();
        BigDecimal jgc = sjcb.getYearCzJgc();
        BigDecimal costHj = sjcb.getYearCostHj();
        // 与上面一致
        BigDecimal bnpdlr = com.punai.common.utils.NumberUtil.subtract(czHj, costHj);
        sjcb.setYearLr(bnpdlr);
        BigDecimal bnpdlrl = com.punai.common.utils.NumberUtil.divideUseProportion(bnpdlr, 2, com.punai.common.utils.NumberUtil.add(czHj, jgc));
        sjcb.setYearLrl(bnpdlrl);

        // 累计利润考核情况分析 与上面一致
        BigDecimal klCzYsHj = sjcb.getKlCzYsHj();
        BigDecimal klCzJgc = sjcb.getKlCzJgc();
        BigDecimal klCostHj1 = sjcb.getKlCostHj();
        BigDecimal kllr = com.punai.common.utils.NumberUtil.subtract(klCzYsHj, klCostHj1);
        sjcb.setKlLr(kllr);
        BigDecimal kllrl = com.punai.common.utils.NumberUtil.divideUseProportion(kllr, 2, NumberUtil.add(klCzYsHj, klCzJgc));
        sjcb.setKlLrl(kllrl);

    }

}
